package cn.edu.zafu.demo.api;

import android.os.Handler;
import android.os.Looper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.edu.zafu.demo.ui.bean.News;

/**
 * @author lizhangqu
 * @since 2016-02-21 11:40
 */
public class NewsApi {

    private static Handler handler = new Handler(Looper.getMainLooper());
    private static Random random = new Random();

    public static void loadNews(final boolean pullToRefresh, final Listener<List<News>> listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final List<News> list = new ArrayList<News>();
                News news1 = new News("标题1", "描述描述描述描述描述描述描述描述描述描述描述描述1");
                News news2 = new News("标题2", "描述描述描述描述描述描述描述描述描述描述描述描述2");
                News news3 = new News("标题3", "描述描述描述描述描述描述描述描述描述描述描述描述3");
                News news4 = new News("标题4", "描述描述描述描述描述描述描述描述描述描述描述描述4");
                News news5 = new News("标题5", "描述描述描述描述描述描述描述描述描述描述描述描述5");
                News news6 = new News("标题6", "描述描述描述描述描述描述描述描述描述描述描述描述6");

                list.add(news1);
                list.add(news2);
                list.add(news3);
                list.add(news4);
                list.add(news5);

                if (pullToRefresh) {
                    list.add(news6);
                }


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (listener != null) {
                            int i = random.nextInt(100);
                            if (i % 2 == 0) {
                                listener.onSuccess(list);
                            } else {
                                listener.onFailure(1000);
                            }
                        }

                    }
                });
            }
        }).start();

    }

}
